//1.双指针---移动零---数组划分区间 
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        //[0, dest] [dest + 1, i - 1] [i, n-1]
        //非0元素        0元素           待遍历
        int dest = -1, i = 0, n = nums.size();
        for(; i < n; i++)
        {
            if(nums[i]) 
                swap(nums[i], nums[++dest]);
        }
    }
};



//2.双指针---复写零
class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        //1.找到复写位置
        //1.1 先判断cur位置的值
        //1.2 决定dest走一步还是两步
        //1.3 判断dest是否已经到最后位置
        //1.4 cur++
        int dest = -1, cur = 0, n = arr.size();
        while(cur < n)
        {
            if(arr[cur]) dest++;
            else dest += 2;
            if(dest >= n - 1) break;
            cur++;
        }
        //2.处理边界情况(防止越界报错)
        if(dest == n)
        {
            arr[n - 1] = 0;
            dest -= 2, cur--;
        }
        //3.从后向前复写
        while(cur >= 0)
        {
            if(arr[cur])
            {
                arr[dest] = arr[cur];
                dest--;
            }
            else 
            {
                arr[dest] = arr[dest - 1] = 0;
                dest -= 2;
            }
            cur--;
        }
    }
};


